package group2assignone.view;

import group2assignone.func.UCCtoALAdapter;
import group2assignone.model.Person;
import group2assignone.ucm.UCChooseLogin;
import group2assignone.ucm.UCListFlights;
import group2assignone.ucm.customer.UCUICustomerRegister;
import group2assignone.ucm.customer.UCUISearchFlights;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EtchedBorder;

/**
 * Shows the main welcome view for the program
 * @author group2
 */
public class MainView extends OurView {

    private JLabel mainLab = new JLabel("Welcome to Group2 Flights");
    private JLabel uNameLab = new JLabel("User Name: ");
    private JLabel pwordLab = new JLabel("Password: ");
    private JTextField usrName = new JTextField(15);
    private JPasswordField password = new JPasswordField(15);
    private JButton login = new JButton("Login");
    private JButton searchFlights = new JButton("Search Flights");
    private JButton listFlights = new JButton("List all Flights");
    private JButton createAccount = new JButton("Create Account");
    private CheckboxGroup cbg = new CheckboxGroup();
    private JPanel fPanel = new JPanel();
    private JPanel fPanel2 = new JPanel();

    private JTextField loginLabel = new JTextField("");
/**
 * add specific components to default panels
 */
    @Override
    public void addComponents() {

        this.setTitle("Group2 Flights Main Menu");
        northPanel.add(mainLab);

        fPanel.setLayout(new GridLayout(2, 2, 10, 10));

        fPanel.add(uNameLab);
        fPanel.add(usrName);
        fPanel.add(pwordLab);
        fPanel.add(password);

        fPanel2.add(new Checkbox("General", cbg, true));
        fPanel2.add(new Checkbox("Manager", cbg, false));
        fPanel2.add(new Checkbox("Administrator", cbg, false));

        centerPanel.setLayout(new BorderLayout());
        centerPanel.add(fPanel, BorderLayout.NORTH);
        centerPanel.add(fPanel2, BorderLayout.SOUTH);

        southPanel.add(searchFlights);
        southPanel.add(listFlights);
        southPanel.add(login);
        southPanel.add(createAccount);
        centerPanel.add(loginLabel);

        centerPanel.setBorder(BorderFactory.createEmptyBorder(20, 10, 20, 20));
        northPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
        southPanel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

        login.addActionListener(new UCCtoALAdapter(new UCChooseLogin(this)));
        searchFlights.addActionListener(new UCCtoALAdapter(new UCUISearchFlights(this)));
        listFlights.addActionListener(new UCCtoALAdapter(new UCListFlights(this)));
        createAccount.addActionListener(new UCCtoALAdapter(new UCUICustomerRegister(this)));
    }

    /**
     * sets login details to parameter p (person)
     * @param p
     */
    public void upload(Person p) {
        p.setPassword(String.copyValueOf(password.getPassword()));
        p.setUsername(usrName.getText());
    }

    public void setLoadText(String s)
    {
        loginLabel.setText(s);
        repaint();
    }
    /**
     * get the type of login from the checkbox
     * @return login type (string)
     */
    public String getType() {
        System.out.println(cbg.getSelectedCheckbox().getLabel());
        return cbg.getSelectedCheckbox().getLabel();
    }
}

